<?php
namespace Admin\Controller;
class ArticleCatController extends AdminController
{

    public function _initialize(){
        parent::_initialize();
        $this->assign('sys', 'news'); // 数据集
    }
    
    //文章分类 显示
    public function index(){
        $article = M("article_cat");
        $cat = $article->where('show_in_nav = 1')->order("sort asc")->select();
        $this->assign("cat",$cat);
        $this->assign('action', 'category');
        $this->display();
    }


    //新增分类
    public function add(){
        if(IS_POST){
            $data = I('post.');
            $article_cat = M('Article_cat');
            $tf = $article_cat->data($data)->add();
            if($tf){
                $str['path_name'] = '0,'.$tf;
                $article_cat->where(array('cat_id'=>$tf))->save($str);
                $this->success('操作成功',U('index'));
            }else{
                $this->error('操作失败');
            }
        }
        $this->assign('action', 'category');
        $this->display();
    }

    //编辑分类
    public function edit(){
        $where['cat_id'] = I('cat_id');
        $article_cat = M('Article_cat');
        $arr = $article_cat->where(array('cat_id'=>array('neq',$where['cat_id'])))->select();
        $this->assign('arr',$arr);
        $past = $article_cat->where($where)->find();
        $this->assign('past',$past);
        if(IS_POST){
            $data = I('post.');
            $parent = $article_cat->where(array('cat_id'=>$data['parent_id']))->field('path_name')->find();
            if($parent){
                $data['path_name'] = $parent['path_name'].','.$data['id'];
            }else{
                $data['path_name'] = '0,'.$data['id'];
            }
            $tf = $article_cat->where(array('cat_id'=>$data['id']))->data($data)->save();
            if($tf){
                $this->success('操作成功',U('index'));
            }else{
                $this->error('操作失败');
            }
        }
        $this->assign('action', 'category');
        $this->display();
    }

    //删除
    public function del(){
        $Id = I('post.data', 0, 'intval');
        $where['cat_id'] = $Id;
        $article_cat = M('Article_cat');
        $tf = $article_cat->where($where)->delete();
        if($tf){
            $this->success('操作成功',U('index'));
        }else{
            $this->error('操作失败');
        }
    }

}